package middleware

import (
	"gitee.com/chejiangyi/bsfgo/core/base2"
	"gitee.com/chejiangyi/bsfgo/core/utils"
	"github.com/gin-gonic/gin"
	"net/http"
)

type MaxRequestSizeMiddleware struct{}

func (*MaxRequestSizeMiddleware) GetOrder() float64 {
	return -96
}
func (m *MaxRequestSizeMiddleware) Enabled() bool {
	return base2.BsfConfigInstance.GetMaxRequestSizeEnabled()
}
func (*MaxRequestSizeMiddleware) Middleware(r *gin.Engine) {
	// 1. 配置请求体大小限制 (默认 32MB，这里改为 10MB)
	r.MaxMultipartMemory = base2.BsfConfigInstance.GetMaxRequestSizeSize() // 10 MB
	r.Use(func(c *gin.Context) {
		var maxBytes = base2.BsfConfigInstance.GetMaxRequestSizeSize() // 10 MB
		c.Request.Body = http.MaxBytesReader(c.Writer, c.Request.Body, maxBytes)
		c.Next()
	})
	utils.LogUtil.Info("MaxRequestSizeMiddleware已开启,支持单个请求大小限制(%vM)", base2.BsfConfigInstance.GetMaxRequestSizeSize()/1024/1024)
}
